<template>
  <div>
    <h1>地址列表</h1>
    <el-table :data="bannerlist" style="width: 100%,height: 100%">
      <el-table-column prop="bannerid" label="轮播图id" width="180" />
      <el-table-column label="图片" width="180">
        <template #default="scope">
          <img class="img" :src="scope.row.img" />
        </template>
      </el-table-column>
      <el-table-column prop="link" label="图片链接" />
      <el-table-column prop="alt" label="图片提示" width="80" />
      <el-table-column prop="flag" label="是否启用">
        <template #default="scope">
          <el-switch v-model="scope.row.flag" />
        </template>
      </el-table-column>
      <el-table-column label="删除" width="150">
        <template #default="scope">
          <el-popconfirm
            title="确定要删除这条数据吗"
            @confirm="handleDelete(scope.row.bannerid)"
          >
            <template #reference>
              <el-button size="small" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>

      <el-table-column label="是否删除全部数据" width="150">
        <template #default="scope">
          <el-popconfirm title="确定要删除全部数据吗" @confirm="removeall()">
            <template #reference>
              <el-button size="small" type="danger">全部删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import service from "../../../utils/service";
import { Delete } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";

interface ibanner {
  link: number;
  alt: number;
  img: string;
  bannerid: string;
  flag: boolean;
}

let bannerlist = reactive<ibanner[]>([]);

const getbannerlist = () => {
  service({
    url: "/banner/list",
  }).then((res) => {
    if (res.data.code == 200) {
      console.log(res.data);
      bannerlist.length = 0;
      bannerlist.push(...res.data.data);
    }
  });
};
const handleDelete = (bannerid: string) => {
  service({
    url: "/banner/delete",
    params: {
      bannerid,
    },
  }).then((res) => {
    if (res.data.code == "200") {
      ElMessage({
        message: "删除成功！",
        type: "success",
      });
      getbannerlist();
    }
  });
};
const removeall = () => {
  service({
    url: "/banner/removeAll",
  }).then((res) => {
    if (res.data.code == "200") {
      ElMessage({
        message: "删除全部轮播图数据了！",
        type: "success",
      });
      removeall();
    }
  });
};

getbannerlist();
</script>

<style scoped>
.img {
  width: 100px;
  height: 100px;
}
</style>